package com.tech.connect.activity;

import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.support.annotation.Nullable;
import android.text.Html;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.ksy.common.utils.Constants;
import com.ksy.common.utils.MD5Util;
import com.tech.connect.R;

import java.util.regex.Pattern;

public class ForgotPasswordActivity extends BaseActivity {

    private EditText etPhone;
    private EditText etCode;
    private TextView tvCode;
    private EditText etPassword;
    private EditText etPasswordEnter;
    private Button btEnter;
    private Handler handler = new Handler(Looper.getMainLooper());
    private Runnable runnable = new Runnable() {
        @Override
        public void run() {
            currentS--;
            if (currentS <= 0) {
                tvCode.setEnabled(true);
                tvCode.setText(Html.fromHtml("" + "发送验证码" + ""));
                currentS = maxS;
            } else {
                tvCode.setEnabled(false);
                tvCode.setText(Html.fromHtml("" + currentS + "秒后重发" + ""));
                handler.postDelayed(this, 1000);
            }
        }
    };
    private int maxS = 60;
    private int currentS = maxS;


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getHeadBar().setTitle("忘记密码");
        setContentView(R.layout.activity_forgot_password);
        initView();
    }

    private void initView() {
        etPhone = findViewById(R.id.etPhone);
        etCode = findViewById(R.id.etCode);
        tvCode = findViewById(R.id.tvCode);
        etPassword = findViewById(R.id.etPassword);
        etPasswordEnter = findViewById(R.id.etPasswordEnter);
        btEnter = findViewById(R.id.btEnter);
        tvCode.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                handler.removeCallbacks(runnable);
                String num = etPhone.getText().toString();
                if (TextUtils.isEmpty(num)) {
                    showToast("请输入手机号码");
                    return;
                }
                if (!Pattern.matches(Constants.regularPhone, num)) {
                    showToast("请输入正确的手机号码");
                    return;
                }
//                CommonApiUtils.sendCode(num, new BaseEntityOb<String>(getBaseActivity()) {
//                    @Override
//                    public void onStart() {
//                        super.onStart();
//                        showDialog();
//                    }
//
//                    @Override
//                    public void onDataDeal(boolean isSuccess, String data, String error) {
//                        hideDialog();
//                        showToast(error);
                if (true) {
                    handler.post(runnable);
                } else {
                    tvCode.setEnabled(true);
                    tvCode.setText(Html.fromHtml("" + "发送验证码" + ""));
                    currentS = maxS;
                }
//                    }
//                });
            }
        });

        btEnter.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                final String phone = etPhone.getText().toString().trim();
                String code = etCode.getText().toString().trim();
                String pass = etPassword.getText().toString().trim();
                String passEnter = etPasswordEnter.getText().toString().trim();
                if (isEmpty(phone)) {
                    showToast("请输入手机号码");
                    return;
                }
                if (!Pattern.matches(Constants.regularPhone, phone)) {
                    showToast("请输入正确的手机号码");
                    return;
                }
                if (isEmpty(code)) {
                    showToast("请输入验证码");
                    return;
                }
                if (isEmpty(pass)) {
                    showToast("请输入密码");
                    return;
                }
                if (isNotSame(pass, passEnter)) {
                    showToast("两次输入的密码不一致");
                    return;
                }
                String md5Pass = MD5Util.encrypt2MD5String(pass);
            }
        });
    }

    @Override
    protected void onDestroy() {
        if (handler != null) {
            handler.removeCallbacksAndMessages(null);
        }
        super.onDestroy();
    }
}
